package com.czl.app.service.impl;

import com.czl.app.mapper.RoleInfoMapper;
import com.czl.app.mapper.UserInfoMapper;
import com.czl.app.mapper.UserRoleMapper;
import com.czl.app.model.UserInfo;
import com.czl.app.model.UserRole;
import com.czl.app.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md2Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Component
@Service("userService")
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private RoleInfoMapper roleInfoMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public UserInfo getUserInfo(String userName) {
        return userInfoMapper.findFirstByUserName(userName);
    }

    @Override
    public int insert(UserInfo record) {
        return 0;
    }

    @Override
    public Set<String> getRoles(Integer userId) {
        List<String> roleNameList = roleInfoMapper.queryRoleNameByUserId(userId);
        Set<String> roles = new HashSet<>(roleNameList);
        return roles;
    }

    @Override
    @Transactional(noRollbackFor = Exception.class)
    public void addUser(String userName, String password, String roleName) throws Exception {
        Integer roleId = roleInfoMapper.findRoleIdByRoleName(roleName);
        if (roleId == null) {
            throw new Exception();
        }
        String salt = UUID.randomUUID().toString().replace("-", "");
        SimpleHash simpleHash = new SimpleHash(Md2Hash.ALGORITHM_NAME, password, salt, 3);

        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setSalt(salt);
        userInfo.setPassword(simpleHash.toString());
        userInfo.setCreateTime(new Date());
        userInfo.setLastTime(null);
        userInfo.setUserState(Byte.parseByte("0"));
        userInfoMapper.insert(userInfo);
        UserInfo firstByUserName = userInfoMapper.findFirstByUserName(userName);
        UserRole userRole = new UserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(firstByUserName.gerId());
        userRoleMapper.insert(userRole);
    }

    @Override
    public void delUser(String userName) throws Exception {
        Integer userId = userInfoMapper.findFirstByUserName(userName).getUserId();
        if (userId == null) {
            throw new Exception();
        }
        userRoleMapper.delUserRoleInfoByUserId(userId);
        userInfoMapper.delUserInfoByUserId(userId);
    }

    @Override
    public void adminUpadate(String userName) {
        String salt = UUID.randomUUID().toString().replace("-", "");
        SimpleHash simpleHash = new SimpleHash(Md2Hash.ALGORITHM_NAME, "123123", salt, 3);
        userInfoMapper.updPasswordAndSaltByUserName(userName, simpleHash.toString(), salt);
    }

    @Override
    public void selfUpadate(String userName, String password) {
        String salt = UUID.randomUUID().toString().replace("-", "");
        SimpleHash simpleHash = new SimpleHash(Md2Hash.ALGORITHM_NAME, password, salt, 3);
        userInfoMapper.updPasswordAndSaltByUserName2(userName, simpleHash.toString(), salt);
    }

    @Override
    public boolean verifyPassword(String password, String salt, String oldPassword) {
        SimpleHash oldPwdSimpleHash = new SimpleHash(Md2Hash.ALGORITHM_NAME, oldPassword, salt, 3);
        if (password.equals(oldPwdSimpleHash.toString())) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void updDisableAccount(String userName) {
        userInfoMapper.updStateByUserName(userName, 2);
    }

    @Override
    public void updEnableAccount(String userName) {
        userInfoMapper.updStateByUserName(userName, 1);
    }

    @Override
    public void updResetRoles(String userName, String roleName) {
        UserInfo UserInfo = userInfoMapper.findFirstByUserName(userName);
        Integer roleId = roleInfoMapper.findRoleIdByRoleName(roleName);
        System.out.println("---UserId:---"+UserInfo.getUserId()+"---roleId:---"+roleId);
        userRoleMapper.updRoleIdByUserId(UserInfo.getUserId(),roleId);
    }

    @Override
    public UserInfo findUserInfo(String userName) {
        return userInfoMapper.findFirstByUserName(userName);
    }

    @Override
    public List<UserInfo> findUserInfoAll() {
        return userInfoMapper.findUserInfoAll();
    }
}
